$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_gyspj_m")
    const dgrid1 = $NG.getCmpApi("p_form_gyspj_d1")
    const dgrid2 = $NG.getCmpApi("p_form_gyspj_d2")
    const dgrid3 = $NG.getCmpApi("p_form_gyspj_d3")
    const getHeadValue = (str) => mstform.getItem(str).getValue()

    const dataBak = []

    if (page.oType == 'add') {
        setTimeout(() => {
            getJcData()
        })

        mstform.getItem("u_nd").setValue({
            label:new Date().getFullYear(),
            value:new Date().getFullYear(),
        })
    }

    if (page.oType == 'view' || page.oType == 'edit') {
        dataBak[0] = [...dgrid1.getRows()]
        dataBak[1] = [...dgrid2.getRows()]
        dataBak[2] = [...dgrid3.getRows()]
    }

    useUpdateRow(()=>{
        sumAmt3()
    },"p_form_gyspj_d1")

    useUpdateRow(()=>{
        sumAmt3()
    },"p_form_gyspj_d2")

    useUpdateRow(()=>{
        sumAmt3()
    },"p_form_gyspj_d3")

    hideTabsOrKeys()

    useValuesChange(()=>{
        hideTabsOrKeys()
    },"p_form_gyspj_m.u_gyspjfl")

    // 表体3的计算
    function sumAmt3(){
        const rows1 = dgrid1.getRows()
        const rows2 = dgrid2.getRows()
        const rows3 = dgrid3.getRows()
        let lydf = 0
        let zhpj = 0
        let total = 0
        rows1.forEach(el=>{
            if((Number(el.u_zhpjdf) || 0) > (Number(el.u_bzf) || 0)){
                $NG.message("综合评价得分不能超过标准分！")
                el.u_zhpjdf = 0
            }
            lydf += (Number(el.u_zhpjdf) || 0)
        })
        rows2.forEach(el=>{
            zhpj += (Number(el.u_zhpjdf) || 0)
            if((Number(el.u_zhpjdf) || 0) > (Number(el.u_bzf) || 0)){
                $NG.message("综合评价得分不能超过标准分！")
                el.u_zhpjdf = 0
            }
        })
        rows3.forEach(item=>{
            if(item.u_pjys == '履约评价'){
                item.u_df = 0.8 * (Number(lydf) || 0)
            } else  if(item.u_pjys == '综合评价'){
                item.u_df = 0.2 * (Number(zhpj) || 0)
            } else if(item.u_pjys == '总得分'){
                item.u_df = total
            }
            if(item.u_pjys != '总得分'){
                total += (Number(item.u_df) || 0)
            }
            
        })
        dgrid3.refreshView()
    }

    // 获取基础表数据
    function getJcData() {
        const sql1 = `select u_khxm,u_zdkhnr,u_bzf,u_yqjpfbz from p_form_gyspjjcb_d1`
        const sql2 = `select u_khxm,u_zdkhnr,u_bzf,u_yqjpfbz,u_zhpjdf from p_form_gyspjjcb_d2`
        const sql3 = `select u_pjys,u_df,u_sm from p_form_gyspjjcb_d3`
        const zbData = [[sql1, dgrid1], [sql2, dgrid2], [sql3, dgrid3]]
        zbData.forEach((el, ind) => {
            $NG.execServer("SELECT", { sql: el[0] }, (res) => {
                if (res.count == 0) return
                const datas = JSON.parse(res.data).reduce((value, item) => {
                    value.push(item.extendObjects)
                    return value
                }, [])
                dataBak[ind] = [...datas]
                el[1].clearRows().then(() => {
                    el[1].addRows(datas)
                })
            })
        })
    }

    // 处理明细的隐藏和显示
    function hideTabsOrKeys() {
        const u_gyspjfl = mstform.getItem("u_gyspjfl").getValue()
        let showTabs = []
        switch (u_gyspjfl) {
            case '01':// 日常
                // 隐藏综合评价和部分数据
                showTabs = ["tabPanel_tabItem", "tabPanel_tabItem_2"]
                dgrid3.clearRows().then(() => {
                    dgrid3.addRows(dataBak[2].filter(el => {
                        if (el.u_pjys != '综合评价') {
                            return el
                        }
                    }))
                })
                break;
            default:
                showTabs = ["tabPanel_tabItem", "tabPanel_tabItem_1", "tabPanel_tabItem_2"]
                dgrid3.clearRows().then(() => {
                    dgrid3.addRows([...dataBak[2]])
                })
                break;
        }
        const tabs = $NG
            .getCompMap()
            .get("__Tabs__")[0]
            .getApi();

        tabs.updateItems((innerItems, activeKey) => {
            return [...innerItems.filter((item) => {
                if (showTabs.includes(item.name)) {
                    return item
                }
            })];
        });
    }
})
